import Vue from "vue";
import VueRouter from "vue-router";
// 路由懒加载
const Adopt = () => import("@/views/pets/adopt");

// 登录
const Login = () => import("@/views/login");
// 注册
const Register = () => import("@/views/register");
// 忘记密码
const Forget = () => import("@/views/find");
// 首页
const Index = () => import("@/views/home/home");
// 宠物
const Pet = () => import("@/views/pets/pets");
// 宠物分类
const Message = () => import("@/views/message/message");
// 我的
const Mine = () => import("@/views/mine/mine");

// 智能穿戴
const Clothe = () => import("@/views/home/clothe/clothe");
// 宠物洗护
const PetWash = () => import("@/views/home/PetWashing/Petwashing ");
// 附近宠物
const NearPet = () => import("@/views/home/nearPet/nearPet");
// 宠物话题的查看更多
const More = () => import("@/views/home/more/more");
// 商品详情页
const Goods = () => import("@/views/home/clothe/goods");
// 内容详情
const Content = () => import("@/views/home/content");
//养宠秘籍
const Ycmj = () => import("@/views/home/ycmj");
//跳转购物车
const Shoppingcart = () => import("@/views/home/clothe/shoppingCart");
//上传图片
const Sctp = () => import("@/views/pets/sctp");
const details1 = () => import("@/views/home/PetWashing/details1");
// 发布话题
const moreAdd = () => import("@/views/home/more/moreAdd");
// 发布的送养
const Send = () => import("@/views/mine/Send");
// 申请领养
const Application = () => import("@/views/mine/Application");
// 我的收藏
const Collection = () => import("@/views/mine/Collection");
// 我的收藏下得all
const CollectionAll = () => import("@/views/mine/collection/CollectionAll");
// 我的收藏下得list
const CollectionList = () => import("@/views/mine/collection/CollectionList");
// 我的收藏下得login
const CollectionLogin = () => import("@/views/mine/collection/CollectionLogin");
// 购物车
const UserSettings = () => import("@/views/mine/UserSettings.vue");
// 联系我们
const ContactUs = () => import("@/views/mine/ContactUs.vue");
// 编辑资料
const UserProfile = () => import("@/views/mine/UserProfile.vue");
// 足迹
const Footprint = () => import("@/views/mine/Footprint.vue");
// 我的头像
const HeadPortrait = () => import("@/views/mine/user/Login.vue");
// 修改头像
const Phone = () => import("@/views/mine/HeadPortrait/Phone.vue");
// 修改昵称
const UserName = () => import("@/views/mine/HeadPortrait/UserName.vue");
const neardetail = () => import("@/views/home/nearPet/neardetail");
// 分类的宠物详情
const AdoptDetail = () => import("@/components/adoptDetail");
// 宠物乐园的宠物详情
const ReDetail = () => import("@/components/recommendDetail");
// 宠物话题详情
const moredetail = () => import("@/views/home/more/moredetail");
// 搜索
const Search = () => import("@/components/search");
// 分类的宠物详情我的领养
const MyaAdoption = () => import("@/components/MyAdoption.vue");
// 宠物乐园的宠物详情我的领养
const MyaAdoptionAdd = () => import("@/components/MyAdoptionAdd.vue");
// 我的购物车
const Shop = () => import("@/components/shop")
// 赞过
const Praise = () => import("@/views/mine/user/Praise .vue");

Vue.use(VueRouter);

const routes = [
  {
    path: "/register",
    component: Register,
  },
  // 内容详情
  {
    path: "/sctp",
    component: Sctp,
  },
  {
    path: '/shop',
    component: Shop
  },
  {
    path: "/content/:psid",
    component: Content,
  },
  {
    path: "/moredetail/:tid",
    component: moredetail,
  },
  {
    //附近宠物
    path: "/neardetail/:mid",
    component: neardetail,
  },
  {
    path: "/goods/:cid",
    component: Goods,
  },
  // 宠物话题详情
  {
    path: "/moredetail",
    component: moredetail,
  },
  // 商品详情页
  {
    path: "/goods",
    component: Goods,
  },
  // 登录
  {
    path: "/",
    component: Login,
  },
  // 忘记密码
  {
    path: "/find",
    component: Forget,
  },

  // 首页
  {
    path: "/home",
    component: Index,
  },
  // 智能穿戴
  {
    path: "/home/clothe",
    name: "/home/clothe",
    component: Clothe,
  },
  // 宠物洗护
  {
    path: "/home/petwash",
    name: "/home/petwash",
    component: PetWash,
  },
  // 附近宠物
  {
    path: "/home/nearPet",
    name: "/home/nearPet",
    component: NearPet,
  },
  // 宠物话题的查看更多
  {
    path: "/home/more",
    component: More,
  },
  {
    path: "/pet",
    component: Pet,
  },
  {
    path: "/home/more",
    component: More,
  },

  {
    path: "/",
    component: Index,
  },
  {
    path: "/pet",
    component: Pet,
  },
  {
    path: "/adopt",
    component: Adopt,
  },
  // 宠物分类
  {
    path: "/message",
    component: Message,
  },
  // 我的
  {
    path: "/mine",
    component: Mine,
  },
  // 赞过
  {
    path: "/mine/praise ",
    component: Praise,
  },
  // 洗护详情
  {
    path: "/details/:wid",
    component: details1,
  },
  // 发布话题
  {
    path: "/moreAdd",
    component: moreAdd,
  },
  // 发布的送养
  {
    path: "/mine/send",
    component: Send,
  },
  // 申请领养
  {
    path: "/mine/application",
    component: Application,
  },
  // 我的收藏
  {
    path: "/mine/collection",
    component: Collection,
    children: [
      // 我的收藏下得all
      {
        path: "/mine/collection/all",
        component: CollectionAll,
      },
      //我的收藏下得list
      {
        path: "/mine/collection/list",
        component: CollectionList,
      },
      //我的收藏下得login
      {
        path: "/mine/collection/login",
        component: CollectionLogin,
      },
    ],
  },
  // 购物车
  {
    path: "/mine/user-settings",
    component: UserSettings,
  },
  // 联系我们
  {
    path: "/mine/contact-us",
    component: ContactUs,
  },
  // 编辑资料
  {
    path: "/mine/user-profile",
    component: UserProfile,
  },
  // 足迹
  {
    path: "/mine/foot-print",
    component: Footprint,
  },
  // 我的头像
  {
    path: "/registered",
    component: HeadPortrait,
  },
  // 修改头像
  {
    path: "/mine/phone",
    component: Phone,
  },
  // 修改昵称
  {
    path: "/mine/user-name",
    component: UserName,
  },
  // 跳转购物车
  {
    path: "/shoppingcart",
    component: Shoppingcart,
  },
  // 养宠秘籍
  {
    path: "/ycmj",
    component: Ycmj,
  },
  // 分类的宠物详情
  {
    path: "/adoptDetail/:rid",
    component: AdoptDetail,
  },
  // 宠物乐园的宠物详情
  {
    path: "/redetail/:rid",
    component: ReDetail,
  },
  // 搜索
  {
    path: "/search",
    component: Search,
  },
  // 分类的宠物详情我的领养
  {
    path: "/myadoption/:rid",
    component: MyaAdoption,
  },
  // 宠物乐园的宠物详情我的领养
  {
    path: "/MyAdoptionAdd/:rid",
    component: MyaAdoptionAdd,
  },
];


const router = new VueRouter({
  mode: "hash",
  routes,
});

// 登录验证，导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next();
  } else {
    let token = localStorage.getItem('token');
    if (token === null || token === '') {
      next('/');
    } else {
      // next('/login');
      next();
    }
  }
});


export default router;
